#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include "FTGLPolygonFont.h"

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "AWLocalViewport.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWScenario.h"
#include "AWBody.h"
#include "AWBrain.h"
#include "AWSensor.h"
#include "AWAgent.h"
#include "AWWorld.h"

#include "AWMonitorBody.h"

AW_MonitorBody::AW_MonitorBody(Ontology *o,FTKBase *memory) : AW_Body(o,memory) 
{
	class_name->set("AW_MonitorBody");

	operators();

	m_dx=320;
	m_dy=240;
	m_nlines=14;

	font=0;

} /* AW_MonitorBody::AW_MonitorBody */ 


AW_MonitorBody::AW_MonitorBody(FILE *fp,Ontology *o,FTKBase *memory) : AW_Body(o,memory)
{
	class_name->set("AW_MonitorBody");

	operators();

	m_dx=320;
	m_dy=240;
	m_nlines=14;

	font=0;

	load(fp);
} /* AW_MonitorBody::AW_MonitorBody */ 


void AW_MonitorBody::operators(void)
{
	FeatureTerm *op=0;

	m_ontology->new_sort("text-operator","any",1,"text","symbol");
	op=FeatureTerm::fromStringNOOS("(define (text-operator))",m_memory,m_ontology);
	m_ops.Add(op);
} /* AW_MonitorBody::operators */ 


AW_MonitorBody::~AW_MonitorBody()
{
	// I don't know why, but the memory_manager complains about this free:
	if (font!=0) {
//		delete font;
		font=0;
	} /* if */ 
} /* AW_MonitorBody::~AW_MonitorBody */ 


bool AW_MonitorBody::load(FILE *fp) 
{
	if (!AW_Body::load(fp)) return false;


	return true;
} /* AW_MonitorBody::load */ 


bool AW_MonitorBody::save(FILE *fp) 
{
	if (!AW_Body::save(fp)) return false;


	return true;
} /* AW_MonitorBody::save */ 


bool AW_MonitorBody::is_a(char *c)
{
	if (strcmp(c,"AW_MonitorBody")==0) return true;

	return AW_Body::is_a(c);
} /* AW_MonitorBody::is_a */ 


bool AW_MonitorBody::is_a(Symbol *c)
{
	if (c->cmp("AW_MonitorBody")) return true;

	return AW_Body::is_a(c);
} /* AW_MonitorBody::is_a */ 


bool AW_MonitorBody::ApplyOperator(FeatureTerm *op,float time,AW_Agent *a,AW_World *w)
{
	if (strcmp(op->getSort()->get(),"text-operator")==0) {
		Symbol *text;
		char *str;
		text=op->featureValueQuick("text")->getSymbol();

		str=new char[strlen(text->get())+1];
		strcpy(str,text->get());
		m_text.Add(str);
		while(m_text.Length()>m_nlines) delete [](m_text.ExtractIni());
		return true;
	} /* if */ 

	return AW_Body::ApplyOperator(op,time,a,w);
} /* AW_MonitorBody::ApplyOperator */ 



void AW_MonitorBody::draw_body(void)
{

	if (font==0) {
		FILE *fp=fopen("arial.ttf","r");
		if (fp!=0) {
			fclose(fp);
			font=new FTGLPolygonFont("arial.ttf");
			font->FaceSize(2);
			font->Depth(0.2F);
		} /* if */ 
	} /* if */  

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	glTranslatef(0,-float(m_dy)/48,0);

	// Draw the monitor body:
	glColor3f(0.5F,0.5F,0.5F);
	glutSolidBox(float(m_dx)/48,float(m_dy)/48,0.2F);

	glTranslatef(0,0,0.2F);
	glColor3f(0.0F,0.0F,0.0F);
	glutSolidBox(float(m_dx)/50,float(m_dy)/50,0.1F);

	// Draw the text:
	if (font!=0) {
		char *str;

		glTranslatef(float(m_dx)/50-0.25F,0.75F-float(m_dy)/50,0.15F);
		glNormal3f(0,0,1);
		glRotatef(180,0,0,1);
		glScalef(0.33F,0.33F,0.33F);

		m_text.Rewind();
		while(m_text.Iterate(str)) {

			glColor3f(1,1,1);
			glPushMatrix();
			font->Render(str);
			glPopMatrix();
			glTranslatef(0,-2,0);
		} /* while */ 
	} /* if */ 

	glPopMatrix();

} /* AW_MonitorBody::draw_body */ 


